package com.itbuka.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.itbuka.goods.domain.GoodsSpec;
import com.itbuka.goods.mapper.GoodsSpecMapper;
import com.itbuka.goods.service.GoodsSpecService;
import org.springframework.stereotype.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;


import java.util.List;

/**
* @author ld
* @description 针对表【goods_spec(规格表)】的数据库操作Service实现
* @createDate 2024-07-27 14:45:54
*/
@Service
public class GoodsSpecServiceImpl implements GoodsSpecService {
    @Autowired
    private GoodsSpecMapper iGoodsSpecMapper;

    @Override
    public List<GoodsSpec> selectAll() {
        LambdaQueryWrapper <GoodsSpec> lqw = new LambdaQueryWrapper<>();
        lqw.eq(GoodsSpec::getIsDelete,0);
        return iGoodsSpecMapper.selectList(lqw);
    }


    @Override
    public List<GoodsSpec> selectList(GoodsSpec iGoodsSpec) {
        LambdaQueryWrapper <GoodsSpec> lqw = this.lqw(iGoodsSpec);
        return iGoodsSpecMapper.selectList(lqw);
    }

    @Override
    public Integer insert(GoodsSpec iGoodsSpec) {
        return iGoodsSpecMapper.insert(iGoodsSpec);
    }

    @Override
    @Transactional
    public Integer delete(String ids) {
        try {
            String[] split = ids.split(",");
            for (String id : split) {
                GoodsSpec iGoodsSpec = new GoodsSpec();
                iGoodsSpec.setId(Long.parseLong(id));
                iGoodsSpec.setIsDelete(1);
                iGoodsSpecMapper.updateById(iGoodsSpec);
            }
            return 1;
        }catch (Exception e){
            return -1;
        }
    }

    @Override
    public Integer update(GoodsSpec iGoodsSpec) {
        return iGoodsSpecMapper.updateById(iGoodsSpec);
    }

    @Override
    public Page<GoodsSpec> pageAll(Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<GoodsSpec>)this.selectAll();
    }

    @Override
    public Page<GoodsSpec> pageList(GoodsSpec iGoodsSpec, Integer page, Integer size) {
        PageHelper.startPage(page,size);
        return (Page<GoodsSpec>)this.selectList(iGoodsSpec);
    }

    public LambdaQueryWrapper<GoodsSpec> lqw(GoodsSpec iGoodsSpec){
        LambdaQueryWrapper <GoodsSpec> lqw = new LambdaQueryWrapper<>();
        if (iGoodsSpec.getId() != null){
            lqw.eq(GoodsSpec::getId,iGoodsSpec.getId());
        }
        if (iGoodsSpec.getSpecName() != null){
            lqw.eq(GoodsSpec::getSpecName,iGoodsSpec.getSpecName());
        }
        if (iGoodsSpec.getSpecValue() != null){
            lqw.eq(GoodsSpec::getSpecValue,iGoodsSpec.getSpecValue());
        }
        if (iGoodsSpec.getIsDelete() != null){
            lqw.eq(GoodsSpec::getIsDelete,iGoodsSpec.getIsDelete());
        }
        if (iGoodsSpec.getCreateTime() != null){
            lqw.eq(GoodsSpec::getCreateTime,iGoodsSpec.getCreateTime());
        }
        if (iGoodsSpec.getUpdateTime() != null){
            lqw.eq(GoodsSpec::getUpdateTime,iGoodsSpec.getUpdateTime());
        }
        return lqw;
    }
}




